package com.learn.concurrent.aqs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wangxing
 * @version 2020/8/19 22:37 Administrator
 */
public class LearnCountDownLatch {
    public static final int TEN = 10;
    private final static Logger logger = LoggerFactory.getLogger(LearnCountDownLatch.class);
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(TEN);
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 2, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),new ThreadPoolExecutor.CallerRunsPolicy());
        for(int i=0;i<TEN;i++){
            int finalI = i;
            threadPoolExecutor.execute(()->{
                logger.info("------{}-------","线程"+ finalI);
                try {
                    TimeUnit.SECONDS.sleep(2);
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threadPoolExecutor.shutdown();
        logger.info("run end");

    }
}
